#ifndef SOCK_H
#define SOCK_H
#include "common.h"

typedef struct {
	char *buf;
	int offset;
	int size;
	WPF *handler;
	void *arg;
	event_t *ev;
}mem_write_state;

typedef struct {
	int in_fd;
	int out_fd;
	int offset;
	int size;
	int size_send;
	WPF *callback;
	void *data;
	event_t *ev;
}sendfile_state;

typedef struct {
	int fd;
	int retry;
	/* int fd, int error, int res, void *data) */
	void (*cb)(int, int, int, void *); 
	void *cb_data;
	event_t *ev;
}connect_state_t;

typedef struct {
	int fd;
	event_t *ev;
	int (*filter)(const char *, const int, void *);
	void *arg;
}read_state_t;

extern void sock_connect(event_t *ev, int fd, struct sockaddr_in *remote, \
				void *data, void (*cb) (int, int, int, void *));
extern void sock_cork_set(int fd);
extern int sock_open(int reuse, int noblck);
extern void sock_close(int fd);
extern void sock_listen(int fd, int queue);
extern int sock_accept(int fd, struct sockaddr *addr, int size);
extern int sock_bind(int fd, struct sockaddr *addr, int addr_size);
extern int error_ignore(int error_no) ;
extern int sock_noblocking_set(int fd);
extern void sock_write(event_t *ev, int fd, WPF *handler, void *arg, GString *s);
extern void sock_reuseaddr(int fd);
extern void sock_sendfile(event_t *ev, int in, int out, int offset, int size, WPF *callback, void *data);
/* get host ip address */
extern int sock_open_from_host(const char *host, struct in_addr *addr);
/* parse IP_ADDR:PORT string, remaind to prealloc ip size */
extern int sock_ip_port_parse(const char *src, char *ip, int *port);
extern int sock_blocking_set(int fd) ;
/* read function */
extern void sock_read(event_t *ev, int fd, \
				int (*filter)(const char *, const int, void *), void *arg);
#endif
